#include "allreader.h"
#include "ui_allreader.h"
#include <QsqlTableModel>
Allreader::Allreader(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Allreader)
{
    ui->setupUi(this);
    is_editing = true;
    global::read =new Readersql;
    model = new QSqlTableModel(this,global::read->m_db);//设置表
    model->setTable("Reader");
    model->select();

    model->setEditStrategy(QSqlTableModel::OnManualSubmit);

    ui->tableView->setModel(model);
    model->setTable("Reader");
    model->select();
    ui->tableView->resizeColumnsToContents();
    ui->tableView->resizeRowsToContents();
    ui->tableView->setAlternatingRowColors(true);

}

Allreader::~Allreader()
{
    delete ui;
}

void Allreader::set_table_model(QStandardItemModel *model)
{ui->tableView->setModel(model);
 ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);


}

void Allreader::public_edit_clicked()
{on_Edit_clicked();

}

void Allreader::on_Edit_clicked()
{if(is_editing==false)
 {ui->Add->setDisabled(true);
 ui->Delete->setDisabled(true);
 ui->Submit->setDisabled(true);
 ui->Edit->setText(QString("完成编辑"));
 is_editing=true;
 ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }
 else
 {ui->Add->setDisabled(false);
        ui->Delete->setDisabled(false);
        ui->Submit->setDisabled(false);
        ui->Edit->setText(QString("编辑"));
        is_editing=false;
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

 }



}


void Allreader::on_Submit_clicked()
{model->submitAll();
QMessageBox::information(this,tr("提示对话框"),QString("提交成功"),QMessageBox::Ok);
}


void Allreader::on_Recall_clicked()
{model->revertAll();
 model->submitAll();

}


void Allreader::on_Add_clicked()
{while(model->canFetchMore())
    {model->fetchMore();

    }
 int rowCount = model->rowCount();
 model->insertRow(rowCount);
 model->setData(model->index(rowCount,0),"");
 ui->tableView->setEditTriggers(QTableView::DoubleClicked);

 ui->tableView->scrollToBottom();

}


void Allreader::on_Delete_clicked()
{auto index = ui->tableView->currentIndex();
 model->removeRow(index.row());
 ui->tableView->repaint();

}

